home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / sound / rndsnd11.zip / RANDYSND.CPP < prev    next >
C/C++ Source or Header  |  1992-08-22  |  20KB  |  785 lines

  1. /* $Id: randysnd.cpp 1.4 92/08/22 15:36:24 AMc Exp Locker: AMc $
  2.  
  3. $Log:    randysnd.cpp $
  4. Revision 1.4  92/08/22  15:36:24  AMc
  5. Added periodic randomizing option
  6.  
  7. Revision 1.3  92/08/22  12:50:24  AMc
  8. Added option to enable/disable the startup dialog
  9.  
  10. Revision 1.2  92/08/22  11:55:11  AMc
  11. Added into RCS database
  12. */
  13.  
  14. // RandySnd is (C) 1992 Anthony McCarthy
  15. //
  16. // Feel free to use and abuse this program to your own ends.
  17. // However, it is provided FREE, AS-IS and with absolutely NO
  18. // WARRANTY whatsoever. Use it at your own risk.
  19. //
  20. // The above statement is especially appropriate as this is
  21. // my very first program built using MSC++ 7 and Microsoft's
  22. // Foundation Classes. As yet I haven't really got my head
  23. // around how to play with precompiled headers. Also, I ain't
  24. // yet played with the diagnostic services provided by MFC
  25. // and so there may well be memory leaks that I haven't seen
  26. // yet.
  27. //
  28. // Please forgive the proliferation of literal integers and
  29. // strings in the code - this code was a quick hack - they
  30. // should all really be symbolic at least.  Also, I haven't yet
  31. // developed/decided on a personal coding style for C++ so 
  32. // things might feel inconsistent.
  33. //
  34. // If you really like this program, a donation of a few pounds,
  35. // dollars, roubles, francs or whatever would be appreciated, to:
  36. // Anthony McCarthy, 14 Beryl Road, Bedminster, Bristol, BS3 3DH, UK
  37. //
  38. // Having said there's no warranty, please report any bugs, enhancement
  39. // requests or source code criticisms to (in order of preference):
  40. //   amc@beryl.demon.co.uk
  41. //   amccarthy@cix.compulink.co.uk
  42. //   100012.3712@compuserve.com 
  43.  
  44.  
  45. // CAVEAT: if you're trying to recompile this source with the same 
  46. // release of MSC 7.0 as I'm using, the compile will fail. Microsoft 
  47. // failed to include the AfxSig_vh enum in afxmsg.h although it is 
  48. // referenced in the ON_WM_DROPFILES() macro in the same file!  To work 
  49. // around this, I simply changed 'AfxSig_vh' to 'AfxSig_vw' in a copy 
  50. // of afxmsg.h.
  51.  
  52.  
  53. // Change History
  54. // ~~~~~~~~~~~~~~
  55. //
  56. // v1.00 : AMcC : 10June92 : initial creation
  57.  
  58.  
  59.  
  60. # include <afxwin.h>
  61. # include <afxcoll.h>
  62. # include <mmsystem.h>
  63. # include <commdlg.h>
  64. # include <shellapi.h>
  65. # include <stdlib.h>
  66. # include "randysnd.h"
  67.  
  68.  
  69.  
  70. static char scratchBuffer[512];
  71.  
  72.  
  73.  
  74. /*--------------------------------------------------------------------------*/
  75. /*                                                                          */
  76. /*                          C M a i n W i n d o w                           */
  77. /*                                                                          */
  78. /*--------------------------------------------------------------------------*/
  79.  
  80. class CMainDialog : public CModalDialog
  81. {
  82.  private:
  83.  
  84.  
  85.    CPtrList soundsList;
  86.  
  87.    BOOL bFilesChanged,
  88.         bAutoRandomiseChanged;
  89.  
  90.    CString curSysSound;
  91.  
  92.    WORD templateId;
  93.  
  94.    UINT timeout;
  95.    UINT timerId;
  96.  
  97.  public:
  98.    static const char * iniFileName;
  99.    static const char * optionsSection;
  100.  
  101.    CMainDialog(WORD dlgId) : CModalDialog(dlgId) { bFilesChanged = FALSE; 
  102.                                                    bAutoRandomiseChanged = FALSE;
  103.                                                    templateId = dlgId;
  104.                                                    timeout = 0;
  105.                                                    timerId = 0;
  106.                                                  }
  107.    ~CMainDialog();
  108.  
  109.    BOOL OnInitDialog();
  110.  
  111.    BOOL GetIniString(CString section, CString key, CString& value);
  112.    BOOL LoadWavFilesList(const CString & sysSound);
  113.    BOOL SaveWavFilesList(const CString & sysSound);
  114.    BOOL FlushWavFilesList();
  115.    BOOL GetSysSound(CString& sysSound);
  116.    BOOL TryToRandomizeSysSound(CString sysSound);
  117.    BOOL WantStartupDialog(void);
  118.    UINT GetRandomizePeriod(void);
  119.    void DoSysSndSelChange(void);
  120.    void DoRandomize(void);
  121.  
  122.    afx_msg void OnAbout();
  123.    afx_msg void OnDestroy();
  124.    afx_msg void OnSysSndSelChange();
  125.  
  126.    afx_msg void OnFileSelChange();
  127.    afx_msg void OnPlaySound();
  128.    afx_msg void OnAddSoundFile();
  129.    afx_msg void OnRemoveSoundFile();
  130.    afx_msg void OnAutoRandomise();
  131.    afx_msg void OnRandomiseNow();
  132.    afx_msg void OnDropFiles(HDROP hDrop);
  133.    afx_msg void OnTimer(UINT nIDEvent);
  134.  
  135.    DECLARE_MESSAGE_MAP()
  136.  
  137. };
  138.  
  139.  
  140. /*--------------------------------------------------------------------------*/
  141. /*                                                                          */
  142. /*                               C M y A p p                                */
  143. /*                                                                          */
  144. /*--------------------------------------------------------------------------*/
  145.  
  146. class CMyApp : public CWinApp
  147. {
  148.  public:
  149.     int  Run();
  150.     BOOL WantedMinimize();
  151. };
  152.  
  153.  
  154. BOOL CMyApp::WantedMinimize()
  155. {
  156.  return (    m_nCmdShow == SW_SHOWMINIMIZED
  157.           || m_nCmdShow == SW_MINIMIZE
  158.           || m_nCmdShow == SW_SHOWMINNOACTIVE );
  159. }
  160.  
  161.  
  162. int CMyApp::Run()
  163. {
  164.  WORD dlgId;
  165.  
  166.  if (WantedMinimize())
  167.     dlgId = IDD_WORKINPROGRESS;
  168.  else
  169.     dlgId = IDD_MAIN;
  170.  
  171.  CMainDialog mainDialog(dlgId);
  172.  
  173.  mainDialog.DoModal();
  174.  
  175.  return 0;
  176. }
  177.  
  178.  
  179.  
  180. // Static application object... the constructor for
  181. // this kicks everything off. 
  182. //
  183. // This comment is here because even though I find the 
  184. // concept of static intializers simple enough to comprehend,
  185. // its still damned difficult to see where the app 'gets going'.
  186.  
  187. CMyApp myApp;
  188.  
  189.  
  190.  
  191.  
  192. const char * CMainDialog::iniFileName    = "randysnd.ini";
  193. const char * CMainDialog::optionsSection = "Options";
  194.  
  195. BEGIN_MESSAGE_MAP( CMainDialog, CModalDialog )
  196.        ON_COMMAND( ID_ABOUT          , OnAbout           )
  197.     ON_COMMAND( ID_PLAYSOUNDFILE  , OnPlaySound       )
  198.     ON_COMMAND( ID_ADDSOUNDFILE   , OnAddSoundFile    )
  199.     ON_COMMAND( ID_REMOVESOUNDFILE, OnRemoveSoundFile )
  200.     ON_COMMAND( ID_AUTORANDOMISE  , OnAutoRandomise   )
  201.     ON_COMMAND( ID_RANDOMIZENOW   , OnRandomiseNow    )
  202.  
  203.     ON_WM_TIMER( )
  204.  
  205.     ON_WM_DESTROY( )
  206.     ON_WM_DROPFILES( ) // *SIGH* MS omitted the AfxSig_vh enum from afxmsg.h!
  207.                        // To work round this, I modified the ON_WM_DROPFILES()
  208.                        // macro (in afxmsg.h) to use the AfxSig_vw signature.
  209.  
  210.     ON_CBN_SELCHANGE( ID_SYSTEMSOUNDS, OnSysSndSelChange )
  211.     ON_LBN_SELCHANGE( ID_FILESLIST   , OnFileSelChange )
  212. END_MESSAGE_MAP()
  213.  
  214.  
  215. CMainDialog::~CMainDialog()
  216. {
  217.  CString * pCstr;
  218.  
  219.  while (     ! soundsList.IsEmpty() 
  220.          &&  (pCstr = (CString *)soundsList.RemoveHead()) != NULL)
  221.        delete pCstr;
  222. }
  223.  
  224.  
  225. BOOL CMainDialog::GetSysSound(CString& sysSound)
  226. {
  227.  BOOL ok = FALSE;
  228.  CComboBox * cb = (CComboBox *)GetDlgItem(ID_SYSTEMSOUNDS);
  229.  CString * s = (CString *)(soundsList.GetAt(soundsList.FindIndex(cb->GetCurSel())));
  230.  
  231.  
  232.  if (! soundsList.IsEmpty())
  233.     {
  234.      sysSound = *s;
  235.      ok = TRUE;
  236.     }
  237.  
  238.  return ok;
  239. }
  240.  
  241.  
  242. BOOL CMainDialog::GetIniString(CString section, CString key, CString& value)
  243. {
  244.  BOOL ok;
  245.  
  246.  ok = GetPrivateProfileString( section, 
  247.                                key, 
  248.                                "", 
  249.                                value.GetBuffer(256), 
  250.                                256, 
  251.                                iniFileName ) > 0;
  252.  
  253.  value.ReleaseBuffer();
  254.  
  255.  return ok;
  256. }
  257.  
  258.  
  259. BOOL CMainDialog::FlushWavFilesList()
  260. {
  261.  BOOL ok = TRUE;
  262.  
  263.  if (bFilesChanged  ||  bAutoRandomiseChanged)
  264.     if (! curSysSound.IsEmpty())
  265.        SaveWavFilesList(curSysSound);
  266.  
  267.  return ok;
  268. }
  269.  
  270.  
  271. BOOL CMainDialog::LoadWavFilesList(const CString& sysSound)
  272. {
  273.  BOOL ok = TRUE;
  274.  char str[20];
  275.  int ctr;
  276.  CListBox * lb = (CListBox *)GetDlgItem(ID_FILESLIST);
  277.  CButton * but = (CButton *)GetDlgItem(ID_AUTORANDOMISE);
  278.  CString wavName;
  279.  
  280.  
  281.  FlushWavFilesList();
  282.  
  283.  
  284.  lb->ResetContent();
  285.  
  286.  int qty = GetPrivateProfileInt(sysSound, "FileCount", 0, iniFileName);
  287.  
  288.  for ( ctr  = 1;
  289.        ctr <= qty;
  290.        ctr++ )
  291.      {
  292.       wsprintf(str, "SoundFile%d", ctr);
  293.  
  294.       if (GetIniString(sysSound, str, wavName))
  295.          lb->AddString(wavName);
  296.      }
  297.  
  298.  
  299.  but->SetCheck(GetPrivateProfileInt(sysSound, "Randomize", 0, iniFileName));
  300.  
  301.  
  302.  curSysSound = sysSound;
  303.  
  304.  bFilesChanged = FALSE;
  305.  bAutoRandomiseChanged = FALSE;
  306.  
  307.  return ok;
  308. }
  309.  
  310.  
  311. BOOL CMainDialog::SaveWavFilesList(const CString & sysSound)
  312. {
  313.  BOOL ok = TRUE;
  314.  int qty = 0;
  315.  int count = 0;
  316.  CListBox * lb = (CListBox *)GetDlgItem(ID_FILESLIST);
  317.  char key[30];
  318.  char value[10];
  319.  CString filename;
  320.  CButton * but = (CButton *)GetDlgItem(ID_AUTORANDOMISE);
  321.  
  322.  qty = lb->GetCount();
  323.  
  324.  if (qty == LB_ERR)
  325.     qty = 0;
  326.  
  327.  for ( count  = 1;
  328.        count <= qty;
  329.        count++ )
  330.      {
  331.       wsprintf(key, "SoundFile%d", count);
  332.  
  333.       lb->GetText(count - 1, filename);
  334.  
  335.       WritePrivateProfileString(sysSound, key, filename, iniFileName);
  336.      }
  337.  
  338.  wsprintf(value, "%d", qty);
  339.  WritePrivateProfileString(sysSound, "FileCount", value, iniFileName);
  340.  
  341.  wsprintf(value, "%d", but->GetCheck() ? 1 : 0);
  342.  WritePrivateProfileString(sysSound, "Randomize", value, iniFileName);
  343.  
  344.  
  345.  
  346.  
  347.  bFilesChanged = FALSE;
  348.  bAutoRandomiseChanged = FALSE;
  349.  
  350.  return ok;
  351. }
  352.  
  353.  
  354. void CMainDialog::OnDestroy()
  355. {
  356.  if (templateId == IDD_MAIN)
  357.     {
  358.      CButton * cb = (CButton *)GetDlgItem(ID_SHOWEGOBOX);
  359.      UINT randomizePeriod;
  360.      char value[20];
  361.  
  362.      randomizePeriod = GetDlgItemInt( ID_PERIOD,
  363.                                       NULL,
  364.                                       FALSE );
  365.  
  366.      wsprintf(value, "%u", randomizePeriod);
  367.  
  368.      WritePrivateProfileString( optionsSection,
  369.                                 "Period",
  370.                                 value,
  371.                                 iniFileName );
  372.  
  373.      WritePrivateProfileString( optionsSection,
  374.                                 "ShowStartup",
  375.                                 cb->GetCheck() ? "1" : "0",
  376.                                 iniFileName );
  377.  
  378.      FlushWavFilesList();
  379.     }
  380.  
  381.  if (timerId != 0)
  382.     {
  383.      KillTimer(timerId);
  384.      timerId = 0;
  385.     }
  386. }
  387.  
  388.  
  389.  
  390. void CMainDialog::OnDropFiles(HDROP hDrop)
  391. {
  392.  CString sysSound;
  393.  char buffer[129];
  394.  WORD qty;
  395.  CListBox * lb = (CListBox *)GetDlgItem(ID_FILESLIST);
  396.  
  397.  
  398.  if (GetSysSound(sysSound))
  399.     {
  400.      qty = DragQueryFile(hDrop, -1, NULL, 0);
  401.  
  402.      while (qty-- > 0)
  403.            {
  404.             DragQueryFile(hDrop, qty, buffer, sizeof(buffer));
  405.  
  406.             if (buffer[0])
  407.                if (lb->AddString(buffer) != LB_ERR)
  408.                   bFilesChanged = TRUE;
  409.            }
  410.     }
  411.  
  412.  DragFinish(hDrop);
  413. }
  414.  
  415.  
  416. void CMainDialog::OnAutoRandomise()
  417. {
  418.  CButton * but = (CButton *)GetDlgItem(ID_AUTORANDOMISE);
  419.  
  420.  bAutoRandomiseChanged = TRUE;
  421. }
  422.  
  423.  
  424. BOOL CMainDialog::TryToRandomizeSysSound(CString sysSound)
  425. {
  426.  BOOL randomized = FALSE;
  427.  int files;
  428.  int randomFile;
  429.  char filename[129];
  430.  char key[10];
  431.  LPSTR strptr;
  432.  char txt[150];
  433.  
  434.  if (GetPrivateProfileInt(sysSound, "Randomize", 0, iniFileName))
  435.     if (files = GetPrivateProfileInt(sysSound, "FileCount", 0, iniFileName))
  436.        {
  437.         randomFile = ((WORD)rand() % files) + 1;
  438.        
  439.         wsprintf(key, "SoundFile%d", randomFile);
  440.         if (GetPrivateProfileString(sysSound, key, "", filename, sizeof(filename), iniFileName))
  441.            {
  442.             GetProfileString("sounds", sysSound, "", scratchBuffer, sizeof(scratchBuffer));
  443.        
  444.             strptr = scratchBuffer;
  445.             while (*strptr && *strptr != ',')
  446.                   strptr++;
  447.        
  448.             if (*strptr == ',')
  449.                {
  450.                 lstrcpy(txt, filename);
  451.                 lstrcat(txt, strptr);
  452.        
  453.                 WriteProfileString("sounds", sysSound, txt);
  454.        
  455.                 randomized = TRUE;
  456.                }
  457.            }
  458.        }
  459.  
  460.  return randomized;
  461. }
  462.  
  463.  
  464. void CMainDialog::OnTimer(UINT nIDEvent)
  465. {
  466.  if (--timeout == 0)
  467.     {
  468.      DoRandomize();
  469.  
  470.      timeout = GetRandomizePeriod();
  471.  
  472.  
  473.      // Exit if the user has reconfigured the
  474.      // randomizing period to zero...
  475.  
  476.      if (timeout == 0)
  477.         EndDialog(0);
  478.     }
  479. }
  480.  
  481.  
  482. void CMainDialog::DoRandomize()
  483. {
  484.  static char buffer[512];
  485.  LPSTR p;
  486.  BOOL winIniChanged = FALSE;
  487.  
  488.  if (GetProfileString( "sounds", NULL, "", buffer, sizeof(buffer)))
  489.     {
  490.      p = buffer;
  491.  
  492.      while (*p)
  493.            {
  494.             if (TryToRandomizeSysSound(p))
  495.                winIniChanged = TRUE;
  496.  
  497.             p += lstrlen(p) + 1;
  498.            }
  499.  
  500.      if (winIniChanged)
  501.         ::SendMessage((HWND)0xFFFF, WM_WININICHANGE, 0, (LONG)(LPSTR)"sounds");
  502.     }
  503. }
  504.  
  505.  
  506. void CMainDialog::OnRandomiseNow()
  507. {
  508.  FlushWavFilesList();
  509.  
  510.  DoRandomize();
  511.  
  512.  if (myApp.WantedMinimize())
  513.     {
  514.      if (WantStartupDialog())
  515.         {
  516.          DWORD oldtime = GetCurrentTime(),
  517.                newtime;
  518.          MSG msg;
  519.          
  520.          while (oldtime <= (newtime = GetCurrentTime()))
  521.                if (newtime > oldtime + 5000L)
  522.                   break;
  523.                else
  524.                   if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  525.                      if (! IsDialogMessage(&msg))
  526.                         CDialog::PreTranslateMessage(&msg);
  527.         }
  528.  
  529.  
  530.      timeout = GetRandomizePeriod();
  531.  
  532.      if (timeout == 0)
  533.         EndDialog(0);
  534.      else
  535.         {
  536.          SetWindowPos(NULL, -1, -1, 1, 1, 
  537.                        SWP_HIDEWINDOW
  538.                         | SWP_NOACTIVATE
  539.                         | SWP_NOZORDER );
  540.  
  541.          timerId = SetTimer(1, 1000, NULL);
  542.         }
  543.     }
  544. }
  545.  
  546.  
  547. void CMainDialog::OnFileSelChange()
  548. {
  549.  CListBox * lb = (CListBox *)GetDlgItem(ID_FILESLIST);
  550.  CButton * but = (CButton *)GetDlgItem(ID_PLAYSOUNDFILE);
  551.  
  552.  but->EnableWindow(lb->GetCurSel() != LB_ERR);
  553. }
  554.  
  555.  
  556. void CMainDialog::OnPlaySound()
  557. {
  558.  CListBox * lb = (CListBox *)GetDlgItem(ID_FILESLIST);
  559.  CString txt;
  560.  int curSel = lb->GetCurSel();
  561.  
  562.  if (curSel != LB_ERR)
  563.     {
  564.      lb->GetText(curSel, txt);
  565.      sndPlaySound(txt, SND_ASYNC | SND_NODEFAULT);
  566.     }
  567. }
  568.  
  569.  
  570. void CMainDialog::OnRemoveSoundFile()
  571. {
  572.  CListBox * lb = (CListBox *)GetDlgItem(ID_FILESLIST);
  573.  CString txt;
  574.  int curSel = lb->GetCurSel();
  575.  int count;
  576.  
  577.  if (curSel != LB_ERR)
  578.     {
  579.      lb->DeleteString(curSel);
  580.  
  581.      count = lb->GetCount();
  582.  
  583.      if (count != LB_ERR  &&  count > 0)
  584.         {
  585.          if (curSel >= count)
  586.             curSel = count - 1;
  587.  
  588.          lb->SetCurSel(curSel);
  589.         }
  590.  
  591.      bFilesChanged = TRUE;
  592.     }
  593. }
  594.  
  595.  
  596. void CMainDialog::OnAddSoundFile()
  597. {
  598.  CListBox * lb = (CListBox *)GetDlgItem(ID_FILESLIST);
  599.  CString txt;
  600.  int curSel = lb->GetCurSel();
  601.  OPENFILENAME o;
  602.  char buffer[129];
  603.  
  604.  lstrcpy(buffer, "*.wav");
  605.  
  606.  memset(&o, 0, sizeof(o));
  607.  
  608.  o.lStructSize  = sizeof(o);
  609.  o.hwndOwner    = GetSafeHwnd();
  610.  o.lpstrFilter  = "Sound Files (*.wav)\0*.wav\0All Files (*.*)\0*.*\0\0";
  611.  o.nFilterIndex = 1;
  612.  o.lpstrFile    = buffer;
  613.  o.nMaxFile     = sizeof(buffer);
  614.  o.lpstrTitle   = "Add Sound File";
  615.  o.Flags        = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
  616.  
  617.  if (GetOpenFileName(&o))
  618.     {
  619.      if (lb->AddString(o.lpstrFile) != LB_ERR)
  620.         bFilesChanged = TRUE;
  621.     }
  622. }
  623.  
  624.  
  625. void CMainDialog::DoSysSndSelChange()
  626. {
  627.  CComboBox * cb = (CComboBox *)GetDlgItem(ID_SYSTEMSOUNDS);
  628.  
  629.  CString * s = (CString *)(soundsList.GetAt(soundsList.FindIndex(cb->GetCurSel())));
  630.  
  631.  LoadWavFilesList(*s);
  632.  
  633.  curSysSound = *s;
  634. }
  635.  
  636.  
  637. void CMainDialog::OnSysSndSelChange()
  638. {
  639.  DoSysSndSelChange();
  640. }
  641.  
  642. void CMainDialog::OnAbout()
  643. {
  644.  MessageBox( "RandySnd is ⌐ Anthony McCarthy, 1992.\n\n"
  645.              "Feel free to use the program and/or play with the source for your "
  646.              "personal enjoyment. However, re-sale of the executable or use of "
  647.              "the source in a commercial "
  648.              "environment (that is, if you're making any money out of my efforts) "
  649.              "is not permitted without my express permission.\n"
  650.              "\n"
  651.              "This program is free and so is provided with NO WARRANTY WHATSOEVER. "
  652.              "If you're worried about it failing in any way, please delete it now.\n\n"
  653.              "However, I'll gladly accept flames regarding bugs, absent features "
  654.              "and my coding style!\n"
  655.              "\n"
  656.              "Enquiries to (in order of preference):\n"
  657.              "\tamc@beryl.demon.co.uk\n"
  658.              "\tamccarthy@cix.compulink.co.uk\n"
  659.              "\t100012.3712@compuserve.com\n"
  660.              "\n"
  661.              "Oh, and if you really, really like the program, feel free "
  662.              "to send a little donation!",
  663.              "About RandySnd",
  664.              MB_OK | MB_ICONASTERISK );
  665. }
  666.  
  667.  
  668. BOOL CMainDialog::WantStartupDialog()
  669. {
  670.  return GetPrivateProfileInt( optionsSection, 
  671.                               "ShowStartup", 
  672.                               1, 
  673.                               iniFileName) != 0;
  674. }
  675.  
  676.  
  677. UINT CMainDialog::GetRandomizePeriod()
  678. {
  679.  return GetPrivateProfileInt( optionsSection, 
  680.                               "Period", 
  681.                               0, 
  682.                               iniFileName);
  683. }
  684.  
  685.  
  686.  
  687. BOOL CMainDialog::OnInitDialog()
  688. {
  689.  LPSTR p;
  690.  CComboBox * cb = (CComboBox *)GetDlgItem(ID_SYSTEMSOUNDS);
  691.  CButton * chk = (CButton *)GetDlgItem(ID_SHOWEGOBOX);
  692.  char str[150];
  693.  LPSTR strptr;
  694.  
  695.  srand((WORD)GetCurrentTime());
  696.  
  697.  rand(); // I've seen reports of MS' rand() routine starting off
  698.  rand(); // rather badly. So I call it a few times to get it going...
  699.  rand(); // (I haven't analysed it myself but this doesn't hurt, so
  700.          // what the heck...)
  701.  
  702.  
  703.  if (templateId == IDD_MAIN)
  704.     {
  705.      chk->SetCheck(WantStartupDialog());
  706.  
  707.      SetDlgItemInt( ID_PERIOD,
  708.                     GetRandomizePeriod(),
  709.                     FALSE );
  710.  
  711.      GetProfileString( "sounds", 
  712.                        NULL, 
  713.                        "", 
  714.                        scratchBuffer, 
  715.                        sizeof(scratchBuffer) );
  716.      
  717.      p = scratchBuffer;
  718.      
  719.      while (*p)
  720.            {
  721.             GetProfileString("sounds", p, "", str, sizeof(str));
  722.      
  723.             strptr = str;
  724.             while (*strptr && *strptr != ',')
  725.                   strptr++;
  726.      
  727.             if (*strptr)
  728.                {
  729.                 cb->InsertString(-1, strptr + 1 /* skip comma */);
  730.                 soundsList.AddTail(new CString(p)); 
  731.                }
  732.      
  733.             p += lstrlen(p) + 1;
  734.            }
  735.      
  736.      cb->SetCurSel(0);
  737.      cb->SetFocus();
  738.  
  739.      DoSysSndSelChange();
  740.  
  741.      DragAcceptFiles(GetSafeHwnd(), TRUE);
  742.     }
  743.  else  // This is the startup dialog
  744.     {
  745.      RECT wr;
  746.      int dx,
  747.          dy,
  748.          x,
  749.          y;
  750.  
  751.  
  752.      GetWindowRect(&wr);
  753.  
  754.      dx = wr.right  - wr.left;
  755.      dy = wr.bottom - wr.top;
  756.  
  757.      x = (GetSystemMetrics(SM_CXSCREEN) - dx) / 2;
  758.      y = (GetSystemMetrics(SM_CYSCREEN) - dy) / 2;
  759.  
  760.      SetWindowPos( NULL,
  761.                    x,
  762.                    y,
  763.                    0,
  764.                    0,
  765.                    SWP_NOACTIVATE
  766.                     | SWP_NOSIZE
  767.                     | SWP_NOREDRAW
  768.                     | SWP_NOZORDER );
  769.                    
  770.  
  771.      if (! WantStartupDialog())
  772.         SendMessage(WM_COMMAND, ID_RANDOMIZENOW);
  773.      else
  774.         PostMessage(WM_COMMAND, ID_RANDOMIZENOW);
  775.     }
  776.  
  777.  return TRUE;
  778. }
  779.  
  780.  
  781.  
  782.  
  783. /* eof: randysnd.cpp */
  784.  
  785.